package com.baidu.health.Listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Random;
import java.util.concurrent.TimeUnit;

@Component
public class RabbitListeners {

    @Autowired
    private RedisTemplate redisTemplate;
    @RabbitListener(queues="healthcare.queues")
    public void Listener(Message message, String phone, Channel channel) throws IOException {
        long id = message.getMessageProperties().getDeliveryTag();
        try {
            Random random=new Random();
            int i = random.nextInt(1000000);
            String code = String.format("%06d", i);
            //4、使用阿里大于第三方短息发送工具（5分）
            //SMSUtils.sendSMS(phone,code);
            System.out.println("验证码为:"+code);
            //            使用redis确保验证码只在1分钟内有效（5分）
            redisTemplate.opsForValue().set(phone,code,60, TimeUnit.SECONDS);
            channel.basicAck(id,false);
        } catch (Exception e) {
            channel.basicNack(id,false,false);
        }
    }
}
